# 模块用于发送邮件
import smtplib

# 用于处理邮件头部信息
from email.header import Header

# 用于创建包含附件的邮件内容
from email.mime.multipart import MIMEMultipart

# 用于创建纯文本邮件内容
from email.mime.text import MIMEText


def send_mail(trade_date,receivers, subject = '数据采集完成',content = '数据采集完成',mail_pass):
    # 邮件发送方邮箱地址
    sender = "3318550879@qq.com"
    # 邮件接收方地址，注意接收者地址应为列表形式，即使只有一个接收者
    # receivers = ["569118688@qq.com"]

    if receivers is None or not receivers:
        # 如果为none或者空列表
        receivers = [sender]

    # 设置邮件内容
    # msg = MIMEMultipart(content,'html','utf-8')
    msg = MIMEMultipart()
    msg['From'] = sender
    # 如果是多个接收者，用逗号加空格分隔
    msg['To'] = ";".join(receivers)
    # subject = 'Python SMTP 邮件测试'
    msg['Subject'] = Header(str(trade_date) + subject, 'utf-8')

    # 邮件征文内容
    msg.attach(MIMEText(content, 'html', 'utf-8'))

    # 登录邮箱，密码为授权码
    mail_host = "smtp.qq.com"
    mail_user = sender
    

    try:
        # 使用SMTP_SSL加密连接
        # 使用 465 端口
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, msg.as_string())
        smtpObj.quit()  # 发送后记得关闭连接
        # print("success")

    except  smtplib.SMTPResponseException as e:
        print("Error：无法发送邮件，错误信息：", e)



# content = f"""
#      测试执行开始时间：xxx </br>
#      测试用例总数：4 </br>
#      测试执行结束时间：3</br>
#      测试执行时长：3</br>
#      测试通过条数：3</br>
#      测试失败条数：1</br>
#      测试通过率：75.00%</br>
#      """
#
# send_mail("2024-05-20", ["3318550879@qq.com","569118688@qq.com"], subject='pytest自动化测试结果', content=content)
